﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Model;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Data
{
	public class ProductAttributeDal
	{
		IDBHelp db = DBHelp.Create();
		Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
		StringBuilder strSql = new StringBuilder();

		#region 是否存在该记录
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		/// <param name="ProductAttributeID">标识列ID</param>
		public bool Exists(int ProductAttributeID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select count(1) from [ProductAttribute] where ProductAttributeID=@productattributeid ");
			htSyn.Add("@productattributeid", ProductAttributeID);
			return db.IsExists(strSql.ToString(), htSyn);
		}
		#endregion

        #region 根据属性id是否存在该记录
        /// <summary>
        /// 根据属性id是否存在该记录
        /// </summary>
        /// <param name="ProductAttributeID">标识列ID</param>
        public bool Exists(int attributeID,int productID)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select count(1) from [ProductAttribute] where AttributeID=@attributeid and ProductID=@productid");
            htSyn.Add("@attributeid", attributeID);
            htSyn.Add("@productid", productID);
            return db.IsExists(strSql.ToString(), htSyn);
        }
        #endregion

		#region 得到该表数据列表
		/// <summary>
		/// 得到该表数据列表
		/// </summary>
		public IList<ProductAttribute> GetModel()
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("Select [ProductAttributeID],[ProductID],[AttributeID],[ProductAttributeValue],[ProductAttributeUrl] FROM [ProductAttribute]");
			 return getProductAttributeBySql(strSql.ToString());
		}
		#endregion

        #region 根据产品id得到产品的属性数据列表---郑同飞
        /// <summary>
        /// 根据产品id得到产品的属性数据列表---郑同飞
        /// </summary>
        /// <param name="productID"></param>
        /// <returns></returns>
        public DataTable getAllAttribute(int productID)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select * from productattribute,attribute where ProductAttribute.productid=@productid and productattribute.attributeid=attribute.attributeid");
            htSyn.Add("@productid",productID);
            return db.GetDataTable(strSql.ToString(), htSyn);

        }
        #endregion

        #region 根据标识列查询实体
        /// <summary>
		/// 根据标识列查询实体
		/// </summary>
		/// <param name="ProductAttributeID">标识列ID</param>
		/// <returns></returns>
		public ProductAttribute GetModel(int productattributeid)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select [ProductAttributeID],[ProductID],[AttributeID],[ProductAttributeValue],[ProductAttributeUrl] FROM [ProductAttribute]");
			strSql.Append(" where ProductAttributeID=@productattributeid ");
			htSyn.Add("@ProductAttributeID", productattributeid);
			DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
			ProductAttribute productattributeModel = new ProductAttribute();
			if (dt.Rows.Count > 0)
			{
				productattributeModel.productID=Convert.ToInt32(dt.Rows[0]["ProductID"]);
				productattributeModel.attributeID=Convert.ToInt32(dt.Rows[0]["AttributeID"]);
				productattributeModel.productAttributeValue=dt.Rows[0]["ProductAttributeValue"].ToString();
				productattributeModel.productAttributeUrl=dt.Rows[0]["ProductAttributeUrl"].ToString();
			}
			return productattributeModel;
		}
		#endregion

        #region 根据属性id获得产品属性内容---郑同飞
        /// <summary>
        /// 根据属性id获得产品属性内容---郑同飞
        /// </summary>
        /// <param name="attributeID"></param>
        /// <returns></returns>
        public IList<ProductAttribute> getModelByAttributeID(int attributeID,int productID) 
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select [ProductAttributeID],[ProductID],[AttributeID],[ProductAttributeValue],[ProductAttributeUrl] FROM [ProductAttribute]");
            strSql.Append(" where AttributeID=@attributeid ");
            strSql.Append(" and ProductID=@productid");
            htSyn.Add("@attributeid", attributeID);
            htSyn.Add("@productid",productID);
            return getProductAttributeBySql(strSql.ToString(), htSyn);          
        }
        #endregion

        #region 修改信息
        /// <summary>
		/// 修改信息
		/// </summary>
		/// <param name="ProductAttribute">实体</param>
		/// <returns></returns>
		public bool Update(ProductAttribute productattributeModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("update [ProductAttribute] set ");
			strSql.Append("[ProductID]=@productID,");
			strSql.Append("[AttributeID]=@attributeID,");
			strSql.Append("[ProductAttributeValue]=@productAttributeValue,");
			strSql.Append("[ProductAttributeUrl]=@productAttributeUrl");
			strSql.Append(" where ProductAttributeID=@productattributeid ");
			htSyn.Add("@productattributeid", productattributeModel.productAttributeID);
            htSyn.Add("@productID", productattributeModel.productID);
            htSyn.Add("@attributeID", productattributeModel.attributeID);
            htSyn.Add("@productAttributeValue", productattributeModel.productAttributeValue);
            htSyn.Add("@productAttributeUrl", productattributeModel.productAttributeUrl);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 删除信息
		/// <summary>
		/// 删除信息
		/// </summary>
		/// <param name="ProductAttributeID">标识列ID</param>
		/// <returns></returns>
		public bool Delete(int ProductAttributeID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("delete from [ProductAttribute] where ProductAttributeID=@productattributeid");
			htSyn.Add("productattributeid", ProductAttributeID);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 添加信息
		/// <summary>
		/// 添加信息
		/// </summary>
		/// <param name="ProductAttribute">实体</param>
		/// <returns></returns>
		public bool Insert(ProductAttribute productattributeModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("insert into [ProductAttribute]([ProductID],[AttributeID],[ProductAttributeValue],[ProductAttributeUrl]) values (@productid,@attributeid,@productattributevalue,@productattributeurl)");
			htSyn.Add("@productid", productattributeModel.productID);
			htSyn.Add("@attributeid", productattributeModel.attributeID);
			htSyn.Add("@productattributevalue", productattributeModel.productAttributeValue);
			htSyn.Add("@productattributeurl", productattributeModel.productAttributeUrl);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 根据SQL语句获取集合
		/// <summary>
		///根据SQL语句获取集合
		/// </summary>
		/// <param name="strSql">sql语句</param>
		public IList<ProductAttribute> getProductAttributeBySql(string strSql)
		{
			IList<ProductAttribute> list = new List<ProductAttribute>();
			DataTable dt = db.GetDataTable(strSql);
			if (dt.Rows.Count > 0)
			{
				foreach (DataRow dr in dt.Rows)
				{
					ProductAttribute productattributeModel = new ProductAttribute();
					productattributeModel.productAttributeID= Convert.ToInt32(dr["ProductAttributeID"]);
					productattributeModel.productID= Convert.ToInt32(dr["ProductID"]);
					productattributeModel.attributeID= Convert.ToInt32(dr["AttributeID"]);
					productattributeModel.productAttributeValue=dr["ProductAttributeValue"].ToString();
					productattributeModel.productAttributeUrl=dr["ProductAttributeUrl"].ToString();
					list.Add(productattributeModel);
				}
			}
			return list;
		}
		#endregion       

        #region 根据SQL语句获取集合
        /// <summary>
        ///根据SQL语句获取集合
        /// </summary>
        /// <param name="strSql">sql语句</param>
        public IList<ProductAttribute> getProductAttributeBySql(string strSql,Hashtable ht)
        {
            IList<ProductAttribute> list = new List<ProductAttribute>();
            DataTable dt = db.GetDataTable(strSql,ht);
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    ProductAttribute productattributeModel = new ProductAttribute();
                    productattributeModel.productAttributeID = Convert.ToInt32(dr["ProductAttributeID"]);
                    productattributeModel.productID = Convert.ToInt32(dr["ProductID"]);
                    productattributeModel.attributeID = Convert.ToInt32(dr["AttributeID"]);
                    productattributeModel.productAttributeValue = dr["ProductAttributeValue"].ToString();
                    productattributeModel.productAttributeUrl = dr["ProductAttributeUrl"].ToString();
                    list.Add(productattributeModel);
                }
            }
            return list;
        }
        #endregion       

        #region 产品属性分页---郑同飞
        /// <summary>
        /// 产品属性分页---郑同飞
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="attributeID"></param>
        /// <param name="productID"></param>
        /// <returns></returns>
        public DataTable pagingForProductAttribute(int pageIndex,int pageSize,int attributeID,int productID)
        {
            strSql.Remove(0, strSql.Length);
            if (attributeID>0)
            {
                strSql.AppendFormat(" and AttributeID={0}",attributeID);
            }
            if (productID>0)
            {
                strSql.AppendFormat(" and ProductID={0}",productID);
            }
            return db.GetMPageDataTable(pageIndex, pageSize, "[ProductAttributeID],[ProductID],[AttributeID],[ProductAttributeValue],[ProductAttributeUrl]", "[ProductAttribute]", strSql.ToString(), "ProductAttributeID", true);
        }
        #endregion

        #region 获取产品属性数量---郑同飞
        /// <summary>
        /// 获取产品属性数量---郑同飞
        /// </summary>
        /// <param name="attributeID"></param>
        /// <param name="productID"></param>
        /// <returns></returns>
        public int getRecordCount(int attributeID,int productID)
        {
            strSql.Remove(0, strSql.Length);
            htSyn.Clear();
            object objRecordCount = null;
            int i = 0;
            strSql.Append("select count(1) from [ProductAttribute] Where 1>0");
            if (attributeID>0)
            {
                strSql.Append(" and AttributeID=@attributeid");
                htSyn.Add("@attributeid",attributeID);
            }
            if (productID>0)
            {
                strSql.Append(" and ProductID=@productid");
                htSyn.Add("@productid",productID);
            }
            objRecordCount = db.ExecuteScalar(strSql.ToString(),htSyn);
            if (objRecordCount!=null)
            {
                i = Convert.ToInt32(objRecordCount);
            }
            return i;
        }
        #endregion
    }
}
